<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="企业全称" prop="companyName">
        <el-input
            v-model="queryParams.companyName"
            placeholder="请输入企业全称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="企业性质" prop="industryType" >
        <el-select
            v-model="queryParams.industryType"
            placeholder="请选择企业性质"
            clearable
            @change="handleQuery"
            style="width: 200px"
        >
          <el-option
              v-for="item in industryTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="企业规模" prop="companySize" >
        <el-select
            v-model="queryParams.companySize"
            placeholder="请选择企业规模"
            clearable
            @change="handleQuery"
            style="width: 200px"
        >
          <el-option
              v-for="item in companySizeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>


      <el-form-item label="联系人称呼" prop="contactName">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入联系人称呼"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Check"
          :disabled="multiple"
          @click="handleApprove"
          v-hasPermi="['system:application:approve']"
        >通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Close"
          :disabled="multiple"
          @click="handleReject"
          v-hasPermi="['system:application:reject']"
        >拒绝</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
        v-loading="loading"
        :data="applicationList"
        @selection-change="handleSelectionChange"
        fit
        style="width: 100%"
        :show-header-overflow="true"
        :show-overflow-tooltip="true"
        >
      <el-table-column type="selection" align="center" fixed="left" :resizable="false"/>
      <el-table-column label="操作" align="center"  fixed="left"  :resizable="false" class-name="small-padding fixed-width">
        <template #default="scope">
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:application:edit']">修改</el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:application:remove']">删除</el-button>-->
          <el-button link type="primary" @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
<!--      <el-table-column label="申请ID" align="center" prop="id" />-->
      <el-table-column label="审核状态" align="center" prop="auditStatus"  min-width="100">
        <template #default="scope">
          <el-text
              :type="getStatusTagType(scope.row.auditStatus)"
              disable-transitions>
            {{ scope.row.auditStatus }}
          </el-text>
        </template>
      </el-table-column>
      <el-table-column label="联系人称呼" align="center" prop="contactName" min-width="120"/>
      <el-table-column label="联系电话" align="center" prop="contactPhone" min-width="120"/>
      <el-table-column label="企业logo" align="center" prop="companyLogo" min-width="120"/>
      <el-table-column label="企业全称" align="center" prop="companyName" min-width="200"/>
      <el-table-column label="从事行业" align="center" prop="businessIndustry" min-width="120"/>
      <el-table-column label="企业性质" align="center" prop="industryType" min-width="120"/>
      <el-table-column label="企业规模" align="center" prop="companySize" min-width="120"/>
      <el-table-column label="所在地区" align="center" prop="region" min-width="150"/>
      <el-table-column label="详情地址" align="center" prop="address" min-width="120"/>
      <el-table-column label="企业简介" align="center" prop="companyProfile" min-width="120"/>
      <el-table-column label="企业联系电话" align="center" prop="companyPhone" min-width="120"/>
      <el-table-column label="申请时间" align="center" prop="applicationTime" min-width="150">
        <template #default="scope">
          <span>{{ parseTime(scope.row.applicationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商会成员入会申请对话框 -->
<!--    <el-dialog :title="title" v-model="open" width="400px" append-to-body>-->
<!--      <el-form ref="applicationRef" :model="form" :rules="rules" label-width="80px">-->
<!--        <el-form-item label="联系人称呼" prop="contactName">-->
<!--          <el-input v-model="form.contactName" placeholder="请输入联系人称呼" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="联系电话" prop="contactPhone">-->
<!--          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="企业logo" prop="companyLogo">-->
<!--          <el-input v-model="form.companyLogo" placeholder="请输入企业logo" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="企业全称" prop="companyName">-->
<!--          <el-input v-model="form.companyName" placeholder="请输入企业全称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="从事行业" prop="businessIndustry">-->
<!--          <el-input v-model="form.businessIndustry" placeholder="请输入从事行业" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="所在地区" prop="region">-->
<!--          <el-input v-model="form.region" placeholder="请输入所在地区" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="详情地址" prop="address">-->
<!--          <el-input v-model="form.address" placeholder="请输入详情地址" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="企业简介" prop="companyProfile">-->
<!--          <file-upload v-model="form.companyProfile"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="企业联系电话" prop="companyPhone">-->
<!--          <el-input v-model="form.companyPhone" placeholder="请输入企业联系电话" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="申请时间" prop="applicationTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.applicationTime"-->
<!--            type="date"-->
<!--            value-format="YYYY-MM-DD"-->
<!--            placeholder="请选择申请时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="企业相册" prop="companyAlbum">-->
<!--          <el-input v-model="form.companyAlbum" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <template #footer>-->
<!--        <div class="dialog-footer">-->
<!--          <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--          <el-button @click="cancel">取 消</el-button>-->
<!--        </div>-->
<!--      </template>-->
<!--    </el-dialog>-->
  </div>

<!-- 弹出框 -->
  <el-dialog
      :title="'商会成员详情'"
      v-model="detailVisible"
      width="500px"
      class="right-dialog"
      append-to-body
  >
    <div class="detail-card" style="font-size: small; line-height: 1.8">
      <div class="detail-header">
        <span class="label">审核状态：</span>
        <span class="value">
        <el-tag :type="getStatusTagType(detailForm.auditStatus)" disable-transitions>
          {{ detailForm.auditStatus }}
        </el-tag>
      </span>
      </div>

      <div class="detail-content" style="margin-top: 20px">
        <div class="content-item">
          <span class="label">企业全称：</span>
          <span class="value">{{ detailForm.companyName }}</span>
        </div>

        <div class="content-item">
          <span class="label">联系人称呼：</span>
          <span class="value">{{ detailForm.contactName }}</span>
        </div>

        <div class="content-item">
          <span class="label">联系电话：</span>
          <span class="value">{{ detailForm.contactPhone }}</span>
        </div>

        <div class="content-item">
          <span class="label">企业性质：</span>
          <span class="value">{{ detailForm.industryType }}</span>
        </div>

        <div class="content-item">
          <span class="label">企业规模：</span>
          <span class="value">{{ detailForm.companySize }}</span>
        </div>

        <div class="content-item">
          <span class="label">所在地区：</span>
          <span class="value">{{ detailForm.region }}</span>
        </div>

        <div class="content-item">
          <span class="label">详情地址：</span>
          <span class="value">{{ detailForm.address }}</span>
        </div>

        <div class="content-item">
          <span class="label">企业联系电话：</span>
          <span class="value">{{ detailForm.companyPhone }}</span>
        </div>

        <div class="content-item">
          <span class="label">申请时间：</span>
          <span class="value">{{ parseTime(detailForm.applicationTime) }}</span>
        </div>

        <div class="content-text">
          <span class="label">企业简介：</span>
          <span class="value">{{ detailForm.companyProfile }}</span>
        </div>

        <div class="content-images" v-if="detailForm.companyLogo" style="margin-top: 20px">
          <div class="label">企业LOGO：</div>
          <div class="image-list" style="margin-top: 5px">
            <el-image
                :src="detailForm.companyLogo"
                style="max-width: 200px; max-height: 100px;"
                fit="contain"
            ></el-image>
          </div>
        </div>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleApprove(detailForm.id)">通过</el-button>
        <el-button type="danger" @click="handleReject(detailForm.id)">拒绝</el-button>
        <el-button @click="detailVisible = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="Application">
import { listApplication, getApplication, delApplication, addApplication, updateApplication,approveApplication,rejectApplication } from "@/api/system/application.js"

const { proxy } = getCurrentInstance()

const applicationList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const industryTypeOptions = ref([])
const companySizeOptions = ref([])

const detailVisible = ref(false)
const detailForm = ref({
  id: null,
  auditStatus: null,
  contactName: null,
  contactPhone: null,
  companyLogo: null,
  companyName: null,
  businessIndustry: null,
  industryType: null,
  companySize: null,
  region: null,
  address: null,
  companyProfile: null,
  companyPhone: null,
  applicationTime: null
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    contactName: null,
    contactPhone: null,
    companyName: null,
    industryType: null,
    companySize: null,
  },
  rules: {
    auditStatus: [
      { required: true, message: "审核状态不能为空", trigger: "change" }
    ],
    contactName: [
      { required: true, message: "联系人称呼不能为空", trigger: "blur" }
    ],
    contactPhone: [
      { required: true, message: "联系电话不能为空", trigger: "blur" }
    ],
    companyName: [
      { required: true, message: "企业全称不能为空", trigger: "blur" }
    ],
    industryType: [
      { required: true, message: "企业性质不能为空", trigger: "change" }
    ],
    region: [
      { required: true, message: "所在地区不能为空", trigger: "blur" }
    ],
    address: [
      { required: true, message: "详情地址不能为空", trigger: "blur" }
    ],
    applicationTime: [
      { required: true, message: "申请时间不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ],
  }
})

//审核状态颜色
function getStatusTagType(status) {
  switch(status) {
    case '已通过':
      return 'success';
    case '已拒绝':
      return 'info';
    case '未审核':
      return 'danger';
    default:
      return '';
  }
}

const { queryParams, form, rules } = toRefs(data)

/** 查询商会成员入会申请列表 */
function getList() {
  loading.value = true
  listApplication(queryParams.value).then(response => {
    applicationList.value = response.rows
    total.value = response.total

    const allIndustryTypes = response.rows.map(item => item.industryType)
    const uniqueIndustryTypes = [...new Set(allIndustryTypes)].filter(type => type)
    industryTypeOptions.value = uniqueIndustryTypes.map(type => ({
      value: type,
      label: type
    }))

    const allCompanySizes = response.rows.map(item => item.companySize)
    const uniqueCompanySizes = [...new Set(allCompanySizes)].filter(size => size)
    companySizeOptions.value = uniqueCompanySizes.map(size => ({
      value: size,
      label: size
    }))

    loading.value = false
  }).catch(() => {
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    auditStatus: null,
    contactName: null,
    contactPhone: null,
    companyLogo: null,
    companyName: null,
    businessIndustry: null,
    industryType: null,
    companySize: null,
    region: null,
    address: null,
    companyProfile: null,
    companyPhone: null,
    applicationTime: null,
    createTime: null,
    updateTime: null,
    companyAlbum: null
  }
  proxy.resetForm("applicationRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加商会成员入会申请"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getApplication(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改商会成员入会申请"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["applicationRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateApplication(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addApplication(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除商会成员入会申请编号为"' + _ids + '"的数据项？').then(function() {
    return delApplication(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/application/export', {
    ...queryParams.value
  }, `application_${new Date().getTime()}.xlsx`)
}

getList()

// 查看详情方法 - 确保设置当前ID
function handleView(row) {
  detailForm.value = JSON.parse(JSON.stringify(row));
  detailVisible.value = true;
  // 设置当前选中ID
  ids.value = [row.id];
}

/** 通过按钮操作 */
function handleApprove() {
  const _ids = ids.value
  if (!_ids || _ids.length === 0) {
    proxy.$modal.msgWarning("请先选择要通过的申请")
    return
  }

  proxy.$modal.confirm('确认要通过选中的' + _ids.length + '条申请吗？').then(function() {
    return approveApplication(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("操作成功")
  }).catch(() => {})
}

/** 拒绝按钮操作 */
function handleReject() {
  const _ids = ids.value
  if (!_ids || _ids.length === 0) {
    proxy.$modal.msgWarning("请先选择要拒绝的申请")
    return
  }

  proxy.$modal.confirm('确认要拒绝选中的' + _ids.length + '条申请吗？').then(function() {
    return rejectApplication(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("操作成功")
  }).catch(() => {})
}
</script>

<style scoped>
.detail-card {
  padding: 0 15px;
}

.label {
  font-weight: bold;
  color: #606266;
  min-width: 100px;
  display: inline-block;
}

.value {
  color: #333;
}
.content-item {
  margin-bottom: 12px;
}
.content-images .image-list {
  display: block;
  margin-left: 100px;
}
.dialog-footer {
  text-align: center;
  padding: 15px 0 0;
}
.el-tag {
  margin-left: 5px;
}

</style>